﻿using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.NetWork.Base;
using HR60_PanelSoft.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HR60_PanelSoft.NetWork.Sender
{
    class Sender_0111:BaseSender
    {

        public Sender_0111(byte panelNo, Panel panel,int pbNum, PanelButton panelButton)
        {
            byte[] data = new byte[20];

            int i =0;
            Device d = panelButton.getMappingDevice();
            Scene s = panelButton.getMappingScene();

                    if (d != null)
                    {
                        string tag = d.type.protocolTag;

                        if(CollectionUtils.isBlank(d.getConnnectedPins())){

                            LogUils.info("Sender_0111","按键没有关联设备");
                            return ;
                        }
                            
                        data[i++] = Convert.ToByte(tag, 10);
                        data[i++] = (byte)(d.getConnnectedPins()[0].index+1);

                        
                    } else if(s!= null){

                         

                        SceneType st = s.type;

                        data[i++] = (byte)((int)st + 30);

                        data[i++] = (byte)(Proj.currentProj.scenes.IndexOf(s)+1);
                    }else{

                         LogUils.info("Sender_0111","按键没有关联设备:"+panelButton);
                         return;
                    }
                    string name = panelButton.displayName;
                    if (name == null)
                    {
                        name = "uknown";
                    }
                      //  throw new Exception("panelButton is null;panleid:" + panel.index + ",device:" + d.nickName);
                   
                    byte[] nameByte = ByteUils.china2Hex(name);
                    int length = nameByte.Length;

                    if (length > 14)
                    {
                        length = 14;
                       
                    }
                    
                

                    data[i++] = (byte)length;
                    for(int j=0;j<length;j++)
                    {
                        data[i++] = nameByte[j];

                    }

                if(d!=null){    
                  object [] ret = d.getConnnectedPins()[0].getPanelAndNet();
                  Panel p =  (Panel)ret[0];
                  Net n =  (Net)ret[1];
                  data[17] = (byte)(n.index + 0);
                  data[18] = (byte)(p.index + 0);
                  data[19] = (byte)(getGolobDeviceNo(n,d) + 1);       
                }
              
            this.package = Package.getPackage(0x01, 0x11, panelNo, (byte)pbNum, data);

        }


       
    }
}
